package com.hghivln.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hghivln.constants.Constants;
import com.hghivln.mapper.RoleMapper;
import com.hghivln.pojo.domain.Role;
import com.hghivln.pojo.dto.RoleDto;
import com.hghivln.pojo.vo.AjaxResult;
import com.hghivln.service.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService{

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public AjaxResult listRoleForPage(RoleDto roleDto) {
        Page<Role> page = new Page<>(roleDto.getPageNum(), roleDto.getPageSize());
        QueryWrapper<Role> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(roleDto.getRoleName()),Role.COL_ROLE_NAME,roleDto.getRoleName());
        qw.like(StringUtils.isNotBlank(roleDto.getRoleCode()), roleDto.getRoleCode(), roleDto.getRoleCode());
        qw.eq(StringUtils.isNotBlank(roleDto.getStatus()),Role.COL_STATUS,roleDto.getStatus());
        qw.ge(roleDto.getBeginTime() != null,Role.COL_CREATE_TIME,roleDto.getBeginTime());
        qw.le(roleDto.getEndTime() != null,Role.COL_CREATE_TIME,roleDto.getEndTime());
        qw.eq(Role.COL_DEL_FLAG,0);
        qw.select("role_id","role_name","role_code","role_sort","remark","status","create_time");
        qw.orderByAsc(Role.COL_ROLE_SORT);
        roleMapper.selectPage(page,qw);
        return AjaxResult.success("查询成功",page.getRecords(),page.getTotal());
    }

    @Override
    public AjaxResult addRole(RoleDto roleDto) {
        Role role = new Role();
        BeanUtil.copyProperties(roleDto,role);
        role.setCreateBy(roleDto.getSimpleUser().getUserName());
        role.setCreateTime(DateUtil.date());
        return AjaxResult.success(roleMapper.insert(role));
    }

    @Override
    public AjaxResult updateRole(RoleDto roleDto) {
        Role role = new Role();
        BeanUtil.copyProperties(roleDto,role);
        role.setUpdateBy(roleDto.getSimpleUser().getUserName());
        return AjaxResult.success(roleMapper.updateById(role));
    }

    @Override
    public AjaxResult deleteRoleByIds(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        if (idList.size()>0) {
            // 根据角色 id 删除 sys_role_menu
            roleMapper.deleteRoleMenuByRoleIds(idList);
            // 根据角色 id 删除 sys_role_user
            roleMapper.deleteRoleUserByRoleIds(idList);
            return AjaxResult.success(roleMapper.deleteRoleByIds(idList));
        }
        return AjaxResult.toAjax(0);
    }

    @Override
    public AjaxResult getRoleById(Long id) {
        return AjaxResult.success(roleMapper.getRoleById(id));
    }

    @Override
    public void saveRoleMenu(Long roleId, Long[] menuIds) {
        List<Long> menuIdList = Arrays.asList(menuIds);
        for (Long menuId : menuIdList) {
            roleMapper.saveRoleMenu(roleId, menuId);
        }
    }

    @Override
    public AjaxResult selectAllRole() {
        QueryWrapper<Role> qw = new QueryWrapper<>();
        qw.eq(Role.COL_STATUS, Constants.STATUS_TRUE);
        qw.eq(Role.COL_DEL_FLAG,0);
        qw.orderByAsc(Role.COL_ROLE_SORT);
        return AjaxResult.success(roleMapper.selectList(qw));
    }

    @Override
    public AjaxResult selectRoleIdsByUserId(Long userId) {
        return AjaxResult.success(roleMapper.selectRoleIdsByUserId(userId));
    }

    @Override
    public void saveRoleUser(Long userId,Long[] roleIds) {
        List<Long> roleIdList = Arrays.asList(roleIds);
        for (Long roleId : roleIdList) {
            roleMapper.saveRoleUser(userId,roleId);
        }
    }
}
